#include <stdio.h>
#include <string.h>

#include "thread.h"
#include "device.h"



static mt_err_t init(struct mt_device *device)
{
    char str[10];
    int i;
    
    memset(str, 0, sizeof(str));
    snprintf(str, sizeof(str), "%s\r\n", __FUNCTION__);
    for(i=0; str[i]!=0; i++)
    {
        printf("%c", str[i]);
        mt_delay(10);
    }
    
    return MT_EOK;
}

static mt_err_t open(struct mt_device *device, mt_uint32_t flags)
{
    char str[10];
    int i;
    
    memset(str, 0, sizeof(str));
    snprintf(str, sizeof(str), "%s\r\n", __FUNCTION__);
    for(i=0; str[i]!=0; i++)
    {
        printf("%c", str[i]);
        mt_delay(10);
    }
    
    return MT_EOK;
}

static mt_err_t close(struct mt_device *device)
{
    char str[10];
    int i;
    
    memset(str, 0, sizeof(str));
    snprintf(str, sizeof(str), "%s\r\n", __FUNCTION__);
    for(i=0; str[i]!=0; i++)
    {
        printf("%c", str[i]);
        mt_delay(10);
    }
    
    return MT_EOK;
}

static mt_err_t ioctl(struct mt_device *device, mt_uint32_t cmd, void *arg)
{
    char str[10];
    int i;
    
    memset(str, 0, sizeof(str));
    snprintf(str, sizeof(str), "%s\r\n", __FUNCTION__);
    for(i=0; str[i]!=0; i++)
    {
        printf("%c", str[i]);
        mt_delay(10);
    }
    
    return MT_EOK;
}

static mt_ssize_t pread(struct mt_device *device, void *buffer, mt_size_t size, mt_uint32_t pos)
{
    char str[10];
    int i;
    
    memset(str, 0, sizeof(str));
    snprintf(str, sizeof(str), "%s\r\n", __FUNCTION__);
    for(i=0; str[i]!=0; i++)
    {
        printf("%c", str[i]);
        mt_delay(10);
    }
    
    return size;
}

static mt_ssize_t pwrite(struct mt_device *device, const void *buffer, mt_size_t size, mt_uint32_t pos)
{
    char str[10];
    int i;
    
    memset(str, 0, sizeof(str));
    snprintf(str, sizeof(str), "%s\r\n", __FUNCTION__);
    for(i=0; str[i]!=0; i++)
    {
        printf("%c", str[i]);
        mt_delay(10);
    }
    
    return size;
}

static mt_device_ops_t ops =
{
    .init = init,
    .open = open,
    .close = close,
    .ioctl = ioctl,
    .pread = pread,
    .pwrite = pwrite,
};

static mt_device_t test_dev =
{
    .magic = 0x01,
    .flags = MT_DEVICE_RDWR, 
    .ops = &ops,  
};

static void *thread_func(void *arg)
{
    mt_device_t *device;
    mt_uint8_t buffer[10];
    char *p = arg;
    
    printf("%s\r\n", p);
    while(1)
    {
        device = mt_device_open("test_dev", MT_DEVICE_RDWR);
        if(device == MT_NULL)
            printf("open device faied\r\n");
        
        if(mt_device_read(device, buffer, sizeof(buffer)) != sizeof(buffer))
            printf("read device faied\r\n");
        
        if(mt_device_write(device, buffer, sizeof(buffer)) != sizeof(buffer))
            printf("write device faied\r\n");
        
        if(mt_device_ioctl(device, 0x01, MT_NULL) != MT_EOK)
            printf("control device faied\r\n");
        
        if(mt_device_pread(device, buffer, sizeof(buffer), 0) != sizeof(buffer))
            printf("pread device faied\r\n");
        
        if(mt_device_pwrite(device, buffer, sizeof(buffer), 0) != sizeof(buffer))
            printf("pwrite device faied\r\n");
        
        if(mt_device_close(device) != MT_EOK)
            printf("close device faied\r\n");
        
        mt_delay(1000);
    }
}

int device_test(void)
{
    mt_thread_t *thread1_handle;
    mt_thread_t *thread2_handle;
    
    if(mt_device_register(&test_dev, "test_dev") != MT_EOK)
    {
        printf("register device faied\r\n");
        return -1;
    }
    
    thread1_handle = mt_thread_create("thread1", 
                                      thread_func, 
                                      "thread1", 
                                      1024, 
                                      1, 
                                      5);
    thread2_handle = mt_thread_create("thread2", 
                                      thread_func, 
                                      "thread2", 
                                      1024, 
                                      10, 
                                      5);
    if((thread1_handle == MT_NULL) || (thread2_handle == MT_NULL))
    {
        printf("create thrtead faied\r\n");
        return -1;
    }
    mt_thread_start(thread1_handle);
    mt_thread_start(thread2_handle);
    
    return 0;
}
